<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class CommentController extends Controller
{

	// 前台影评
    public function comment(Request $request)
    {

    	date_default_timezone_set('PRC');

    	$this -> validate($request, 
    	[
    		'total' => 'required',
    		'content' => 'required|max:600'
    	]);	

    	$data    = $request -> except('_token');
    	$total   = $data['total'];
    	$score   = $data['total'];
    	$pid     = $data['pid'];
    	$auth    = $data['auth'];
    	$uid     = $data['uid'];
    	$content = $data['content'];
    	$ctime   = date('Y-m-d H:i:s', time());
    	
    	
    	$res = DB::table('post') -> where('pid', $pid) -> insert(['pid' => $pid, 'uid' => $uid, 'auth' => $auth, 'content' => $content, 'ctime' => $ctime, 'score' => $score]);
    	if($res)
    	{
    		// 查询旧的电影总分数
    		$oldTotal = DB::table('types') -> where('id', $pid) -> first() -> total;
    		// 计算电影新的总分数
    		$newTotal = $oldTotal + $total;
    		// 查询本电影总评论条数
    		$comments = DB::table('post') -> where('pid', $pid) -> count();
    		// 计算电影平均分数
    		$score = round($newTotal / $comments, 1);
    		$totalRes = DB::table('types') -> where('id', $pid) -> update(['total' => $newTotal, 'score' => $score]);
    		if($totalRes)
    		{
    			return back() -> with(['info' => '发表成功']);
    		}else
    		{
    			return back() -> with(['info' => '发表失败']);
    		}
    	}else
    	{
    		return back() -> with(['info' => '发表失败']);
    	}
    	
    }

    // 前台回复影评
    public function reply(Request $request)
    {

    	date_default_timezone_set('PRC');
    	$this -> validate($request, 
    	[
    		'content' => 'required|max:600'
    	]);	

    	// 回复时间
    	$ctime = date('Y-m-d H:i:s', time());
    	$data = $request -> except('_token');
    	$data['ctime'] = $ctime;
    	$replyRes = DB::table('reply') -> insert($data);
    	if($replyRes)
    	{
    		// 查询就的post回复量count
    		$oldCount = DB::table('post') -> where('id', $data['pid']) -> first() -> count;
    		// 计算新的回复量
    		$newCount = $oldCount + 1;
    		$postRes = DB::table('post') -> where('id', $data['pid']) -> update(['count' => $newCount]);
    		if($postRes)
    		{
    			return back() -> with(['info' => '回复成功']);
    		}else
    		{
    			return back() -> with(['info' => '回复失败']);
    		}
    	}else
    	{
    		return back() -> with(['info' => '回复失败']);
    	}

    }

    // 前台影评点赞
    public function couns(Request $request)
    {
        $postId = $request -> nid;
        $oldData = DB::table('post') -> where('id', $postId) -> first();
        $pid = $oldData -> pid;
        // 获取旧的点赞量
        $oldCouns = $oldData -> couns;
        $newCouns = $oldCouns + 1;
        $res = DB::table('post') -> where('id', $postId) -> update(['couns' => $newCouns]);
        if($res)
        {
            return response() -> json($newCouns);
        }else
        {
            return response() -> json('a');
        }
    }
}
